package com.qianfeng.openplatform.geteway.fiter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.qianfeng.openplatform.commons.field.RedisKeyPrefix;
import com.qianfeng.openplatform.commons.field.ResponseMessage;
import com.qianfeng.openplatform.commons.field.StaticField;
import com.qianfeng.openplatform.commons.field.util.Md5Util;
import com.qianfeng.openplatform.geteway.client.CacheClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author 江江
 * @Date 2020/7/27 19:51
 * http://www.openapi.sys/openapi?method=servertest04&name=lisi&name1=lis&admin=msmsms23330&adminkey=123&appkey=561AC1A8676CFCB0CC61B041AE42ABB8
 * 1系统参数管理 开启appkey只能开这个否则需要后台更改appkey
 * 2.应用管理创建一个 ==> 输入网址,加上 appkey ===>后台查看 GateWayStartApp :9876/ 控制器代码
 * 把DED5B07FD21A17D70B978C57E29E2AC1============================= 复制到redis 下面的键值中 然后登陆用redis上面的STRING
 */
//@Component
public class SecretKeyCheckFilter extends ZuulFilter {
    @Autowired
    private CacheClient cacheClient;

    @Override
    public String filterType() { //指定当前过滤器的类型
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() { //指定过滤器的执行顺序
        return FilterConstants.PRE_DECORATION_FILTER_ORDER + 3;
    }

    @Override
    public boolean shouldFilter() {// 开启当前过滤器
        return RequestContext.getCurrentContext().sendZuulResponse();
    }

    @Override // 测试数据http://localhost:9876/openapi?method=servertest01
    public Object run() throws ZuulException {
        //指定过滤器中的具体业务代码
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();
        Map<String, String> map = new HashMap<>();
        Enumeration<String> names = request.getParameterNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            String value = request.getParameter(name);
            map.put(name, value);
        }
        String appkey = request.getParameter("appkey");
        if (StringUtils.isEmpty(appkey)){
            return null;
        }
        String md5Signature = Md5Util.md5Signature(map, appkey);
        System.out.println(md5Signature+"=============================");
        String scerite = null;
        try {
            scerite = cacheClient.getFromRedis(appkey);
            if (!md5Signature.equalsIgnoreCase(scerite)){
                throw  new RuntimeException();
            }
        } catch (Exception e) {
            try {
            requestContext.setSendZuulResponse(false);
            ObjectMapper objectMapper = new ObjectMapper();
            HttpServletResponse response = requestContext.getResponse();
            response.setContentType("application/json;charset=utf-8");
                response.getWriter().write(objectMapper.writeValueAsString(new ResponseMessage(StaticField.REQEST_PARAM_ERROR_CODE,"MD5校验失败")));
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }

        return null;
    }













}
